<?php
class Search extends Frontend_Controller{
	public function __construct(){
		parent::__construct();
		$this->load->library('pagination');
		$this->load->Model(array("Marticle"));
	}	
	
	public function index(){
		$params = array();
		$data['title'] = 'Tìm kiếm';
		if(isset($_GET['q']) && $_GET['q'] != ''){
			$query = $_GET['q'];
			$data['title'] .= ' từ khóa "'.$query.'"';
			$params = $_GET;
			$data['base']=$this->config->item('base_url');

	        $total=$this->Marticle->countAll($params);
	        $data['title'] .= ' có '.$total.' kết quả.';
	        $per_pg=1;
	       	$offset = ($this->uri->segment(2) == '') ? 0 : $this->uri->segment(2);
	        
	        $config['base_url'] = $data['base'].'tim-kiem/';
	        $config['total_rows'] = $total;
	        $config['per_page'] = $per_pg;
	        $config['suffix'] = '?q='.$query;

	        $this->pagination->initialize($config);
	        $data['pagination']=$this->pagination->create_links();

	        $data['items']=$this->Marticle->showArticles($per_pg,$params,$offset);

		}

		$this->parser->parse("search/index.tpl", NULL, $data, FALSE, FALSE);
	}

}